package components.contorls.superClass
{
    import components.core.UIComponent;
    import components.core.zn_internal;
    
    import flash.display.DisplayObject;
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.utils.Dictionary;
    
    import mx.events.FlexEvent;

    [Event(name="selectionChange", type="mx.events.FlexEvent")]
    public class BaseRadioButton extends UIComponent
    {
        private var _label:String = "radioButton";

        private var _radioGroup:BaseRadioGroup;

        private var _selected:Boolean = false;

        private var _value:String;

        public function BaseRadioButton()
        {
            super();
			
			selectedButton.mouseEnabled=selectedButton.mouseChildren=false;
        }

        /***************************************
         *
         * 显示对象声明
         *
         * **************************************/

        public function get labelTF():BaseLabel
        {
            return getChildByName("labelSkin") as BaseLabel;
        }

        public function get selectedButton():BaseButton
        {
            return getChildByName("selectedButtonSkin") as BaseButton;
        }

        public function get selectedDisplay():DisplayObject
        {
            return getChildByName("selectedSkin") as DisplayObject;
        }

        /***************************************
         *
         * 验证
         *
         * **************************************/

        protected override function commitProperties():void
        {
            super.commitProperties();

			if (!_initialized)
			{
				labelTF.verticalCenter=0;
				labelTF.left=labelTF.x;
				labelTF.right=0;
				
				selectedButton.verticalCenter=0;
				
				selectedDisplay.visible=false;
			}
			
            if (labelTF)
                labelTF.text = label;
			
        }

        protected override function measureSizes():Boolean
        {
            var change:Boolean= super.measureSizes();
			
			if(change)
			{
				selectedDisplay.y=height/2-selectedDisplay.height/2;
			}
			
			return change;
        }


        protected override function updateDisplayList():void
        {
            super.updateDisplayList();
        }

        /***************************************
         *
         * 组件定义
         *
         * **************************************/

        protected override function frameConstructedHandler(event:Event):void
        {
            super.frameConstructedHandler(event);
            selected = _selected;
        }

        public function get selected():Boolean
        {
            return _selected;
        }

        public function set selected(value:Boolean):void
        {
			if(_selected==value)
				return ;
			
            _selected = value;

            if (value && radioGroup && radioGroup.selectedRaidoButton != this)
            {
                radioGroup.selectedRaidoButton = this;
            }

			if(selected)
				selectedDisplay.visible=true;
			else
				selectedDisplay.visible=false;
			
            dispatchEvent(new FlexEvent(FlexEvent.SELECTION_CHANGE));
        }

        [Inspectable(defaultValue="RadioButton")]
        public function get label():String
        {
            return _label;
        }

        public function set label(value:String):void
        {
            _label = value;
            invalidate();
        }

        [Inspectable(defaultValue="")]
        public function get value():String
        {
            return _value;
        }

        public function set value(v:String):void
        {
            _value = v;
            invalidate();
        }

        /***************************************
         *
         * 其他代码
         *
         * **************************************/

        public function get radioGroup():BaseRadioGroup
        {
            return _radioGroup;
        }

        zn_internal function set radioGroup(value:BaseRadioGroup):void
        {
            _radioGroup = value;
        }

        override protected function mouseClickHandler(event:MouseEvent):void
        {
            super.mouseClickHandler(event);

            if (radioGroup && radioGroup.selectedRaidoButton == this)
                return;

            selected = !selected;
        }
		
		protected override function rollOverHandler(event:MouseEvent):void
		{
			super.rollOverHandler(event);
			selectedButton.isOver=true;
			selectedButton.setOverState();
		}
		
		protected override function rollOutHandler(event:MouseEvent):void
		{
			super.rollOutHandler(event);
			selectedButton.isOver=false;
			selectedButton.setOutState();
		}
		
		protected override function mouseDownHandler(event:MouseEvent):void
		{
			super.mouseDownHandler(event);
			selectedButton.setDownState();
		}
		
		protected override function mouseUpHandler(event:MouseEvent):void
		{
			super.mouseUpHandler(event);
			selectedButton.setUpState();
		}

        public override function set focusComp(value:UIComponent):void
        {
            super.focusComp = value;

            labelTF.focusComp = focusComp;
        }

        public override function destroy():void
        {
            super.destroy();
            _label = null;
            _radioGroup.removeRadioButton(this);
            _radioGroup = null;
        }

        public override function validateNow():void
        {
            super.validateNow();
            labelTF.validateNow();

        }
    }
}